package nccloud.listener.pu.task;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import nc.ws.opm.pub.utils.CommonUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import nc.bs.dao.BaseDAO;
import nc.bs.framework.common.InvocationInfoProxy;
import nc.bs.framework.common.NCLocator;
import nc.bs.pub.pa.PreAlertObject;
import nc.bs.pub.taskcenter.BgWorkingContext;
import nc.bs.pub.taskcenter.IBackgroundWorkPlugin;
import nc.itf.pu.m20.IPraybillMaintain;
import nc.itf.pubapp.pub.smart.IBillQueryService;
import nc.jdbc.framework.processor.MapListProcessor;
import nc.vo.pu.m20.entity.PraybillVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.pub.lang.UFDate;
import nc.vo.pub.lang.UFDateTime;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nccloud.framework.web.json.JsonFactory;
import nccloud.message.util.MessageCenter;
import nccloud.message.vo.NCCMessage;
import nccloud.message.vo.NCCNoticeMessageVO;

/**
 * 请购单自动关闭并发送消息
 * 
 * @author humf
 * @date 2023年10月17日 下午4:52:53
 */
public class PraybillMessageSyncOATaskplugin implements IBackgroundWorkPlugin {

	public BaseDAO baseDAO = null;
	private IBillQueryService billQuery = NCLocator.getInstance().lookup(IBillQueryService.class);

	public BaseDAO getBaseDAO() {
		if (null == baseDAO) {
			baseDAO = new BaseDAO();
		}
		return baseDAO;
	}

	@Override
	public PreAlertObject executeTask(BgWorkingContext gwc) throws BusinessException {
		String time = (String) gwc.getKeyMap().get("time");
		// 查询请购单,审批日期后四个月以上，且为状态审批通过。
		String qgsql = "SELECT\n" + "vbillcode,\n" + "creator,\n" + "taudittime,\n" + "pk_praybill \n" + "FROM\n"
				+ "po_praybill\n" + "WHERE\n" + "to_date(taudittime) < add_months(sysdate,-" + time + ")\n"
				+ "AND fbillstatus = 3\n";
		List<Map<String, String>> list = (List<Map<String, String>>) getBaseDAO().executeQuery(qgsql,
				new MapListProcessor());

		for (int i = 0; i < list.size(); i++) {
			String vbillcode = list.get(i).get("vbillcode");
			String creator = list.get(i).get("creator");
			String pkpraybill = list.get(i).get("pk_praybill");

			PraybillVO aggvo = billQuery.querySingleBillByPk(PraybillVO.class, pkpraybill);// 直接查询aggVO

			IPraybillMaintain ieft = NCLocator.getInstance().lookup(IPraybillMaintain.class);
			// 关闭请购单// 关闭请购单
			ieft.closeBill(new PraybillVO[] { aggvo });

			// 发送通知消息
			sendMessage(vbillcode, time, creator);
		}
		return null;

	}

	/**
	 * 需要审核时发送通知 add by hmf
	 * 
	 * @param oldPsndocVO
	 * @param psndocVO
	 * @param param
	 */
	private void sendMessage(String billno, String time, String sqr) {
		NCCMessage nccMessage = new NCCMessage();
		NCCNoticeMessageVO messageVO = new NCCNoticeMessageVO();
		messageVO.setMsgtype("nc");
		messageVO.setSender("NC_USER0000000000000");
		messageVO.setContenttype("text/plain");
		messageVO.setPk_group(InvocationInfoProxy.getInstance().getGroupId());
		messageVO.setPk_org(null);
		messageVO.setSendtime(new UFDateTime());
		messageVO.setSendstate(UFBoolean.FALSE);
		messageVO.setPriority(5);
		messageVO.setSubject("您的" + billno + "号请购单已自动关闭");
		messageVO.setContent("审批时间超过" + time + "个月的单据将自动关闭");

		messageVO.setMsgsourcetype("notice");
		nccMessage.setIsread(UFBoolean.TRUE);
		nccMessage.setMessageType("notice");
		nccMessage.setMessage(messageVO);
		try {
			messageVO.setReceiver(sqr);
			MessageCenter.sendMessage(new NCCMessage[] { nccMessage });
		} catch (Exception e) {

		}

	}

}
